package cn.mrcode.fd.utils.log.track;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 日志追踪过滤器
 * @author : zhuqiang
 * @version : V1.0
 * @date : 2016/2/2 21:04
 */
public class LogTrackFilter implements Filter {


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest requeest = (HttpServletRequest) servletRequest;
        if (!ignore(requeest)) {
            LogTrackUtil.start_web(requeest);
            filterChain.doFilter(servletRequest, servletResponse);
            LogTrackUtil.end_web();
        } else {
            filterChain.doFilter(servletRequest, servletResponse);
        }
    }

    /**
     * 忽略处理
     * @return true：不对该次请求做日志追踪处理：只是说不打印开始和结束，一般用于放行静态文件
     */
    protected boolean ignore(HttpServletRequest requeest){
/*
        // 对固定后缀名称做放行处理示意代码
        String ignoreSuffix = "gif,png,js,css,ico,jpg";  //忽略追踪的后缀
        String requestURI = requeest.getRequestURI();
        int i = requestURI.lastIndexOf(".");
        boolean isTrack = false;
        if (i != -1) {
            String suffix = requestURI.substring(i + 1, requestURI.length() - 1);
            if (ignoreSuffix.contains(suffix)) {
                isTrack = true;
            }
        }
        return isTrack;*/
        return false;
    }
    @Override
    public void destroy() {

    }
}
